[[...path]].page.tsx 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. import React, { useEffect } from 'react';
  2. import { type IPagePopulatedToShowRevision, getIdForRef } from '@growi/core';
  3. import type {
  4. GetServerSideProps, GetServerSidePropsContext,
  5. } from 'next';
  6. import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
  7. import Head from 'next/head';
  8. import superjson from 'superjson';
  9. import { ShareLinkLayout } from '~/components/Layout/ShareLinkLayout';
  10. import GrowiContextualSubNavigationSubstance from '~/components/Navbar/GrowiContextualSubNavigation';
  11. import { DrawioViewerScript } from '~/components/Script/DrawioViewerScript';
  12. import { ShareLinkPageView } from '~/components/ShareLinkPageView';
  13. import type { SupportedActionType } from '~/interfaces/activity';
  14. import { SupportedAction } from '~/interfaces/activity';
  15. import type { CrowiRequest } from '~/interfaces/crowi-request';
  16. import type { RendererConfig } from '~/interfaces/services/renderer';
  17. import type { IShareLinkHasId } from '~/interfaces/share-link';
  18. import type { PageDocument } from '~/server/models/page';
  19. import ShareLink from '~/server/models/share-link';
  20. import {
  21. useCurrentUser, useRendererConfig, useIsSearchPage, useCurrentPathname,
  22. useShareLinkId, useIsSearchServiceConfigured, useIsSearchServiceReachable, useIsSearchScopeChildrenAsDefault, useIsContainerFluid, useIsEnabledMarp,
  23. } from '~/stores/context';
  24. import { useCurrentPageId, useIsNotFound, useSWRMUTxCurrentPage } from '~/stores/page';
  25. import loggerFactory from '~/utils/logger';
  26. import type { NextPageWithLayout } from '../_app.page';
  27. import type { CommonProps } from '../utils/commons';
  28. import {
  29. getServerSideCommonProps, generateCustomTitleForPage, getNextI18NextConfig, skipSSR, addActivity,
  30. } from '../utils/commons';
  31. const logger = loggerFactory('growi:next-page:share');
  32. type Props = CommonProps & {
  33. shareLinkRelatedPage?: IShareLinkRelatedPage,
  34. shareLink?: IShareLinkHasId,
  35. isNotFound: boolean,
  36. isExpired: boolean,
  37. disableLinkSharing: boolean,
  38. isSearchServiceConfigured: boolean,
  39. isSearchServiceReachable: boolean,
  40. isSearchScopeChildrenAsDefault: boolean,
  41. isEnabledMarp: boolean,
  42. drawioUri: string | null,
  43. rendererConfig: RendererConfig,
  44. skipSSR: boolean,
  45. ssrMaxRevisionBodyLength: number,
  46. };
  47. type IShareLinkRelatedPage = IPagePopulatedToShowRevision & PageDocument;
  48. superjson.registerCustom<IShareLinkRelatedPage, string>(
  49. {
  50. isApplicable: (v): v is IShareLinkRelatedPage => {
  51. return v != null
  52. && v.toObject != null
  53. && v.lastUpdateUser != null
  54. && v.creator != null
  55. && v.revision != null;
  56. },
  57. serialize: (v) => { return superjson.stringify(v.toObject()) },
  58. deserialize: (v) => { return superjson.parse(v) },
  59. },
  60. 'IShareLinkRelatedPageTransformer',
  61. );
  62. // GrowiContextualSubNavigation for shared page
  63. // get page info from props not to send request 'GET /page' from client
  64. type GrowiContextualSubNavigationForSharedPageProps = {
  65. page?: IPagePopulatedToShowRevision,
  66. isLinkSharingDisabled: boolean,
  67. }
  68. const GrowiContextualSubNavigationForSharedPage = (props: GrowiContextualSubNavigationForSharedPageProps): JSX.Element => {
  69. const { page, isLinkSharingDisabled } = props;
  70. return (
  71. <GrowiContextualSubNavigationSubstance currentPage={page} isLinkSharingDisabled={isLinkSharingDisabled} />
  72. );
  73. };
  74. const SharedPage: NextPageWithLayout<Props> = (props: Props) => {
  75. useCurrentPathname(props.shareLink?.relatedPage.path);
  76. useIsSearchPage(false);
  77. useIsNotFound(props.isNotFound);
  78. useShareLinkId(props.shareLink?._id);
  79. useCurrentPageId(props.shareLink?.relatedPage._id);
  80. useCurrentUser(props.currentUser);
  81. useRendererConfig(props.rendererConfig);
  82. useIsSearchServiceConfigured(props.isSearchServiceConfigured);
  83. useIsSearchServiceReachable(props.isSearchServiceReachable);
  84. useIsSearchScopeChildrenAsDefault(props.isSearchScopeChildrenAsDefault);
  85. useIsEnabledMarp(props.rendererConfig.isEnabledMarp);
  86. useIsContainerFluid(props.isContainerFluid);
  87. const { trigger: mutateCurrentPage, data: currentPage } = useSWRMUTxCurrentPage();
  88. useEffect(() => {
  89. if (!props.skipSSR) {
  90. return;
  91. }
  92. if (props.shareLink?.relatedPage._id != null && !props.isNotFound) {
  93. mutateCurrentPage();
  94. }
  95. }, [mutateCurrentPage, props.isNotFound, props.shareLink?.relatedPage._id, props.skipSSR]);
  96. const pagePath = props.shareLinkRelatedPage?.path ?? '';
  97. const title = generateCustomTitleForPage(props, pagePath);
  98. return (
  99. <>
  100. <Head>
  101. <title>{title}</title>
  102. </Head>
  103. <div className="dynamic-layout-root justify-content-between">
  104. <nav className="sticky-top">
  105. <GrowiContextualSubNavigationForSharedPage page={currentPage ?? props.shareLinkRelatedPage} isLinkSharingDisabled={props.disableLinkSharing} />
  106. </nav>
  107. <ShareLinkPageView
  108. pagePath={pagePath}
  109. rendererConfig={props.rendererConfig}
  110. page={currentPage ?? props.shareLinkRelatedPage}
  111. shareLink={props.shareLink}
  112. isExpired={props.isExpired}
  113. disableLinkSharing={props.disableLinkSharing}
  114. />
  115. </div>
  116. </>
  117. );
  118. };
  119. SharedPage.getLayout = function getLayout(page) {
  120. return (
  121. <>
  122. <DrawioViewerScript />
  123. <ShareLinkLayout>{page}</ShareLinkLayout>
  124. </>
  125. );
  126. };
  127. function injectServerConfigurations(context: GetServerSidePropsContext, props: Props): void {
  128. const req: CrowiRequest = context.req as CrowiRequest;
  129. const { crowi } = req;
  130. const { configManager, searchService } = crowi;
  131. props.disableLinkSharing = configManager.getConfig('crowi', 'security:disableLinkSharing');
  132. props.isSearchServiceConfigured = searchService.isConfigured;
  133. props.isSearchServiceReachable = searchService.isReachable;
  134. props.isSearchScopeChildrenAsDefault = configManager.getConfig('crowi', 'customize:isSearchScopeChildrenAsDefault');
  135. props.drawioUri = configManager.getConfig('crowi', 'app:drawioUri');
  136. props.rendererConfig = {
  137. isSharedPage: true,
  138. isEnabledLinebreaks: configManager.getConfig('markdown', 'markdown:isEnabledLinebreaks'),
  139. isEnabledLinebreaksInComments: configManager.getConfig('markdown', 'markdown:isEnabledLinebreaksInComments'),
  140. isEnabledMarp: configManager.getConfig('crowi', 'customize:isEnabledMarp'),
  141. adminPreferredIndentSize: configManager.getConfig('markdown', 'markdown:adminPreferredIndentSize'),
  142. isIndentSizeForced: configManager.getConfig('markdown', 'markdown:isIndentSizeForced'),
  143. drawioUri: configManager.getConfig('crowi', 'app:drawioUri'),
  144. plantumlUri: configManager.getConfig('crowi', 'app:plantumlUri'),
  145. // XSS Options
  146. isEnabledXssPrevention: configManager.getConfig('markdown', 'markdown:rehypeSanitize:isEnabledPrevention'),
  147. xssOption: configManager.getConfig('markdown', 'markdown:rehypeSanitize:option'),
  148. attrWhitelist: JSON.parse(crowi.configManager.getConfig('markdown', 'markdown:rehypeSanitize:attributes')),
  149. tagWhitelist: crowi.configManager.getConfig('markdown', 'markdown:rehypeSanitize:tagNames'),
  150. highlightJsStyleBorder: configManager.getConfig('crowi', 'customize:highlightJsStyleBorder'),
  151. };
  152. props.ssrMaxRevisionBodyLength = configManager.getConfig('crowi', 'app:ssrMaxRevisionBodyLength');
  153. }
  154. async function injectNextI18NextConfigurations(context: GetServerSidePropsContext, props: Props, namespacesRequired?: string[] | undefined): Promise<void> {
  155. const nextI18NextConfig = await getNextI18NextConfig(serverSideTranslations, context, namespacesRequired);
  156. props._nextI18Next = nextI18NextConfig._nextI18Next;
  157. }
  158. function getAction(props: Props): SupportedActionType {
  159. let action: SupportedActionType;
  160. if (props.isExpired) {
  161. action = SupportedAction.ACTION_SHARE_LINK_EXPIRED_PAGE_VIEW;
  162. }
  163. else if (props.shareLink == null) {
  164. action = SupportedAction.ACTION_SHARE_LINK_NOT_FOUND;
  165. }
  166. else {
  167. action = SupportedAction.ACTION_SHARE_LINK_PAGE_VIEW;
  168. }
  169. return action;
  170. }
  171. export const getServerSideProps: GetServerSideProps = async(context: GetServerSidePropsContext) => {
  172. const req = context.req as CrowiRequest;
  173. const { crowi, params } = req;
  174. const result = await getServerSideCommonProps(context);
  175. if (!('props' in result)) {
  176. throw new Error('invalid getSSP result');
  177. }
  178. const props: Props = result.props as Props;
  179. try {
  180. const shareLink = await ShareLink.findOne({ _id: params.linkId }).populate('relatedPage');
  181. if (shareLink == null) {
  182. props.isNotFound = true;
  183. }
  184. else {
  185. props.isNotFound = false;
  186. props.isExpired = shareLink.isExpired();
  187. props.shareLink = shareLink.toObject();
  188. // retrieve Page
  189. const Page = crowi.model('Page');
  190. const relatedPage = await Page.findOne({ _id: getIdForRef(shareLink.relatedPage) });
  191. // determine whether skip SSR
  192. const ssrMaxRevisionBodyLength = crowi.configManager.getConfig('crowi', 'app:ssrMaxRevisionBodyLength');
  193. props.skipSSR = await skipSSR(relatedPage, ssrMaxRevisionBodyLength);
  194. // populate
  195. props.shareLinkRelatedPage = await relatedPage.populateDataToShowRevision(props.skipSSR); // shouldExcludeBody = skipSSR
  196. }
  197. }
  198. catch (err) {
  199. logger.error(err);
  200. }
  201. injectServerConfigurations(context, props);
  202. await injectNextI18NextConfigurations(context, props);
  203. await addActivity(context, getAction(props));
  204. return {
  205. props,
  206. };
  207. };
  208. export default SharedPage;